package org.jeecg.modules.demo.ipds.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.ipds.entity.ByteFrameZu;
import org.jeecg.modules.demo.ipds.service.IByteFrameZuService;
import org.jeecg.modules.demo.ipds.service.ITreeByteListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 字节表关联子帧组
 * @Author: jeecg-boot
 * @Date:   2024-03-20
 * @Version: V1.0
 */
@Api(tags="字节表关联子帧组")
@RestController
@RequestMapping("/ipds/byteFrameZu")
@Slf4j
public class ByteFrameZuController extends JeecgController<ByteFrameZu, IByteFrameZuService> {
	@Autowired
	private IByteFrameZuService byteFrameZuService;

	@Autowired
	private ITreeByteListService treeByteListService;
	
	/**
	 * 分页列表查询
	 *
	 * @param byteFrameZu
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "字节表关联子帧组-分页列表查询")
	@ApiOperation(value="字节表关联子帧组-分页列表查询", notes="字节表关联子帧组-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ByteFrameZu>> queryPageList(ByteFrameZu byteFrameZu,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ByteFrameZu> queryWrapper = QueryGenerator.initQueryWrapper(byteFrameZu, req.getParameterMap());
		Page<ByteFrameZu> page = new Page<ByteFrameZu>(pageNo, pageSize);
		IPage<ByteFrameZu> pageList = byteFrameZuService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param byteFrameZu
	 * @return
	 */
	@AutoLog(value = "字节表关联子帧组-添加")
	@ApiOperation(value="字节表关联子帧组-添加", notes="字节表关联子帧组-添加")
	@RequiresPermissions("ipds:byte_frame_zu:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ByteFrameZu byteFrameZu) {
		byteFrameZuService.save(byteFrameZu);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param byteFrameZu
	 * @return
	 */
	@AutoLog(value = "字节表关联子帧组-编辑")
	@ApiOperation(value="字节表关联子帧组-编辑", notes="字节表关联子帧组-编辑")
	@RequiresPermissions("ipds:byte_frame_zu:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ByteFrameZu byteFrameZu) {
		byteFrameZuService.updateById(byteFrameZu);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "字节表关联子帧组-通过id删除")
	@ApiOperation(value="字节表关联子帧组-通过id删除", notes="字节表关联子帧组-通过id删除")
	@DeleteMapping(value = "/deleteById")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		byteFrameZuService.removeById(id);
		return Result.OK("删除成功!");
	}



	 /**
	  *   通过id删除
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "子帧组-通过id,index删除")
	 @ApiOperation(value="子帧组-通过id,index删除", notes="子帧组-通过id,index删除")
	 @DeleteMapping(value = "/delete")
	 public Result<String> delete(@RequestParam(name="byte_id",required=true) String byte_id,@RequestParam(name="index",required=false) Integer index) {
			//如果选择删除了删除第几个子帧组
		 if (index!=null&&!index.equals("")){
			 QueryWrapper<ByteFrameZu> queryWrapper=new QueryWrapper<>();
			 //根据字节占位符查询出所有子帧组
			 queryWrapper.eq("byte_id",byte_id);
			 queryWrapper.orderByAsc("index_sx");
			 List<ByteFrameZu> byteFrameZus = byteFrameZuService.list(queryWrapper);

			 //集合的第几个 index是从1开始的
			 ByteFrameZu byteFrameZu = byteFrameZus.get(index - 1);
			 byteFrameZuService.removeById(byteFrameZu);
			 QueryWrapper<ByteFrameZu> updateQuer = new QueryWrapper<>();

				 //删除之后修改一下顺序
				 updateQuer.eq("byte_id", byte_id);
				 List<ByteFrameZu> byteFrame = byteFrameZuService.list(updateQuer);
				 for (ByteFrameZu frameZus : byteFrame) {
					 if (frameZus.getIndexSx() > index) {
						 Integer indexSx = frameZus.getIndexSx() - 1;
						 frameZus.setIndexSx(indexSx);
					 }
				 }
				 byteFrameZuService.updateBatchById(byteFrameZus);

		 }

		 //没index全删了
			 if (index==null||index.equals("")){
			 QueryWrapper<ByteFrameZu> queryWrapper3 = new QueryWrapper<>();
			 queryWrapper3.eq("byte_id", byte_id);
				 byteFrameZuService.remove(queryWrapper3);
				 if (byteFrameZuService.list(queryWrapper3).size() == 0) {
				 System.out.println(byteFrameZuService.list(queryWrapper3).size() == 0);
				 treeByteListService.removeById(byte_id);
			 }
			 }


		 return Result.OK("删除成功!");
	 }


	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "字节表关联子帧组-批量删除")
	@ApiOperation(value="字节表关联子帧组-批量删除", notes="字节表关联子帧组-批量删除")
	@RequiresPermissions("ipds:byte_frame_zu:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.byteFrameZuService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "字节表关联子帧组-通过id查询")
	@ApiOperation(value="字节表关联子帧组-通过id查询", notes="字节表关联子帧组-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ByteFrameZu> queryById(@RequestParam(name="id",required=true) String id) {
		ByteFrameZu byteFrameZu = byteFrameZuService.getById(id);
		if(byteFrameZu==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(byteFrameZu);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param byteFrameZu
    */
    @RequiresPermissions("ipds:byte_frame_zu:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ByteFrameZu byteFrameZu) {
        return super.exportXls(request, byteFrameZu, ByteFrameZu.class, "字节表关联子帧组");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("ipds:byte_frame_zu:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ByteFrameZu.class);
    }

}
